<template>
  <div class="bigbox">
    <div class="topbox" :style="{ height: isheight + 'px' }">
      <el-form
        :model="form"
        ref="form"
        label-position="left"
        label-width="80px"
        onsubmit="return false"
      >
        <el-col :span="6">
          <el-form-item label="用电企业">
            <FilterSelect2
              v-model="bizOrgNo"
              :optionlist="companyList"
              :valuekey="{ name: 'name', value: 'id' }"
              @changeSelect="companyChange"
            ></FilterSelect2>
          </el-form-item>
        </el-col>
        <el-col :span="6"></el-col>
        <el-col :span="6"></el-col>
        <el-col :span="6">
          <el-form-item>
            <div>
              <button @click="chongzhi" class="button">重置</button>
              <button @click="chaxun" class="button chaxun">查询</button>
            </div>
          </el-form-item>
        </el-col>
      </el-form>
    </div>
    <div class="bodybox">
      <div class="titlebox">
        <div class="text"></div>
        <div
          class="right"
          :style="{ alignItems: 'center', lineHeight: '32.5px' }"
        >
          <button
            type="primary"
            size="small"
            class="bewdao daochu"
            style="margin-right: 20px"
            @click="add('add')"
          >
            新增
          </button>
          <button
            type="primary"
            size="small"
            class="bewdao daochu"
            @click="daochu"
          >
            导出
          </button>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <span
              @click="shuaxin"
              :style="{ cursor: 'pointer', fontSize: '20px' }"
              ><i class="el-icon-refresh-right"></i
            ></span>
          </el-tooltip>
          <el-dropdown trigger="click" @command="command">
            <span class="el-dropdown-link">
              <el-tooltip
                class="item"
                effect="dark"
                content="密度"
                placement="top"
              >
                <span :style="{ cursor: 'pointer', fontSize: '20px' }"
                  ><i class="el-icon-d-caret"></i
                ></span>
              </el-tooltip>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item class="dropdown-list" command="默认"
                >默认</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="中等"
                >中等</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="紧凑"
                >紧凑</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <div class="table-content">
        <template>
          <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            border
            ref="table"
            :size="iissize"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center'
            }"
          >
            <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
            <el-table-column
              type="index"
              align="center"
              label="序号"
              width="150"
            >
            </el-table-column>
            <el-table-column prop="companyName" label="用电企业">
            </el-table-column>
            <el-table-column prop="className" label="设备名称">
            </el-table-column>
            <el-table-column prop="savingAdvice" label="节能建议">
            </el-table-column>
            <el-table-column align="right" prop="savingRate" label="预计节能率">
              <template slot-scope="scope">
                {{ scope.row.savingRate + '%' }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="230">
              <template slot-scope="scope">
                <div class="operate">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="编辑"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-edit"
                      @click="handleEdit(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="删除"
                    placement="top"
                  >
                    <i
                      style="font-size: 14px; color: red; cursor: pointer"
                      class="el-icon-delete"
                      @click="handleDel(scope.row)"
                    ></i
                  ></el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <div class="fenye">
          <el-pagination
            background
            layout="prev, pager, next"
            :current-page.sync="pageobj._pageNo"
            @current-change="getdatalist()"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <nt-dialog
      title="节能建议信息"
      :visible.sync="DialogVisible"
      v-if="DialogVisible"
      width="500px"
      @diaClose="beforeClose"
    >
      <el-form
        :rules="rules"
        ref="form"
        :model="dialogForm"
        label-width="100px"
      >
        <el-form-item label="企业名称" prop="companyId">
          <el-select v-model="dialogForm.companyId">
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备" prop="classId">
          <el-select
            v-model="dialogForm.classId"
            placeholder="选择设备"
            size="small"
            style="width: 100%"
            ref="selectReport"
          >
            <el-option
              class="option"
              :label="MeterPoints.name"
              :value="dialogForm.classId"
              style="height: auto"
            >
              <el-tree
                ref="tree"
                :props="props"
                :highlight-current="true"
                :load="loadNodedevice"
                lazy
                @node-click="handleNodeClick"
              >
                <template slot-scope="{ node, data }">
                  <span
                    :style="{
                      cursor:
                        data.className == 'AirconditionMain' ||
                        data.className == 'CoolingTower' ||
                        data.className == 'Boiler' ||
                        data.className == 'FreezePump' ||
                        data.className == 'CoolingPump' ||
                        data.className == 'DraughtFanCoiler' ||
                        data.className == 'NewDraughtFan' ||
                        data.className == 'Lighting' ||
                        data.className == 'Socket' ||
                        data.className == 'Elevator' ||
                        data.className == 'Pump' ||
                        data.className == 'ExhaustFan' ||
                        data.className == 'ElectricCalorifier' ||
                        data.className == 'SpeElectricDevice' ||
                        data.className == 'ProElectricDevice' ||
                        data.className == 'LightingSocket' ||
                        data.className == 'RollingMillDevice' ||
                        data.className == 'Pipeline' ||
                        data.className == 'Heatingpump' ||
                        data.className == 'Watersuppump' ||
                        data.className == 'EcmEquip'
                          ? 'pointer'
                          : 'no-drop',
                      fontSize: '14px',
                      color: '#606266'
                    }"
                  >
                    {{ node.label }}</span
                  >
                </template>
              </el-tree>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="节能建议" prop="savingAdvice">
          <el-input
            type="textarea"
            resize="none"
            :rows="3"
            v-model="dialogForm.savingAdvice"
          ></el-input>
        </el-form-item>
        <el-form-item label="预计节能率" prop="savingRate">
          <el-input
            type="number"
            clearable
            v-model="dialogForm.savingRate"
          ></el-input>
        </el-form-item>
      </el-form>
      <div style="margin-top: 10px; text-align: center">
        <span slot="footer" class="dialog-footer">
          <el-button @click="beforeClose">取 消</el-button>
          <el-button type="primary" @click="submit">保 存</el-button>
        </span>
      </div>
    </nt-dialog>
  </div>
</template>

<script>
// import { saveAs } from 'file-saver'
import {
  addPageListApi,
  delPageListApi,
  editPageListApi,
  getCompanyListApi,
  getPageListApi
} from '@/api/energy-consumption/Equipment-energy/Energy-saver'
import { getgenjiedianapi, getzijidianapi } from '@/api/monitor'
import { exportexcel } from '@/utils/excel'
import ntDialog from '@/components/nt-dialog.vue'
export default {
  components: { ntDialog },
  name: 'EnergySaver',
  data() {
    return {
      bizOrgNo: '',
      isEdit: 'add',
      DialogVisible: false,
      form: {},
      rules: {
        companyId: [
          { required: true, message: '请选择企业', trigger: 'change' }
        ],
        classId: [{ required: true, message: '请选择设备', trigger: 'change' }],
        savingAdvice: [
          { required: true, message: '请输入节能建议', trigger: 'blur' }
        ],
        savingRate: [
          { required: true, message: '请输入预计节能率', trigger: 'blur' }
        ]
      },
      dialogForm: {},
      isheight: 72,
      MeterPoints: { name: '', id: '' },
      props: {
        label: 'name',
        children: 'children',
        isLeaf: 'leaf'
      },
      iissize: 'medium',
      loading: false,
      tableData: [],
      total: 0,
      pageobj: {
        _pageNo: 0,
        _pageSize: 10
      },
      companyList: []
    }
  },
  async mounted() {
    await this.getCompanyList()
    await this.getdatalist()
  },
  methods: {
    companyChange(val) {
      localStorage.setItem('companyId', val)
    },
    // 获取用电企业列表
    async getCompanyList() {
      const res = await getCompanyListApi({ companyId: this.bizOrgNo })
      console.log(res)
      this.companyList = res.data.list
      const companyId = +localStorage.getItem('companyId')
      if (companyId && this.companyList.find((i) => i.id == companyId)) {
        this.bizOrgNo = companyId
      } else {
        this.bizOrgNo = this.companyList[0].id
      }
    },
    // 初始化
    async getchushihua() {
      this.bizOrgNo = this.companyList[0].id
    },
    // 获取数据
    async getdatalist() {
      this.loading = true
      const res = await getPageListApi({
        companyId: this.bizOrgNo,
        pageNo: this.pageobj._pageNo - 1,
        pageSize: 10
      })
      this.tableData = res.data.list
      this.total = res.data.totalCount
      this.loading = false
    },
    // 刷新列表
    shuaxin() {
      this.getdatalist('')
      this.$message({
        type: 'success',
        message: '列表刷新成功',
        duration: 2000
      })
    },
    chaxun() {
      this.getdatalist()
    },
    // 导出
    daochu() {
      exportexcel(
        '/ecm-microservice-biz/ecm/saving/advice/set/export',
        {},
        '节能建议'
      )
    },
    command(val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },
    async submit() {
      await this.$refs.form.validate()
      const res =
        this.isEdit === 'add'
          ? await addPageListApi(this.dialogForm)
          : await editPageListApi(this.dialogForm)
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: this.isEdit === 'add' ? '新增成功' : '修改成功',
          duration: 2000
        })
        this.getdatalist()
        this.beforeClose()
      } else {
        this.$message({
          type: 'error',
          message: this.isEdit === 'add' ? '新增失败' : '修改失败',
          duration: 2000
        })
      }
      console.log(res)
    },
    async chongzhi() {
      await this.getchushihua()
      this.getdatalist('')
    },
    // 编辑
    async handleEdit(row) {
      console.log(row)
      this.isEdit = 'edit'
      this.$set(this.dialogForm, 'companyId', row.companyId)
      this.$set(this.dialogForm, 'classId', row.classId)
      this.$set(this.MeterPoints, 'name', row.className)
      this.$set(this.dialogForm, 'savingAdvice', row.savingAdvice)
      this.$set(this.dialogForm, 'savingRate', row.savingRate)
      this.$set(this.dialogForm, 'id', row.id)
      this.DialogVisible = true
    },
    async add(type) {
      this.isEdit = type
      this.$set(this.dialogForm, 'companyId', this.companyList[0].id)
      this.DialogVisible = true
    },
    // 树点击
    handleNodeClick(val) {
      if (
        val.className == 'AirconditionMain' ||
        val.className == 'CoolingTower' ||
        val.className == 'Boiler' ||
        val.className == 'FreezePump' ||
        val.className == 'CoolingPump' ||
        val.className == 'DraughtFanCoiler' ||
        val.className == 'NewDraughtFan' ||
        val.className == 'Lighting' ||
        val.className == 'Socket' ||
        val.className == 'Elevator' ||
        val.className == 'Pump' ||
        val.className == 'ExhaustFan' ||
        val.className == 'ElectricCalorifier' ||
        val.className == 'SpeElectricDevice' ||
        val.className == 'ProElectricDevice' ||
        val.className == 'LightingSocket' ||
        val.className == 'RollingMillDevice' ||
        val.className == 'Pipeline' ||
        val.className == 'Heatingpump' ||
        val.className == 'Watersuppump' ||
        val.className == 'EcmEquip'
      ) {
        this.$set(this.MeterPoints, 'name', val.name)
        this.$set(this.dialogForm, 'classId', val.id)
        this.$refs.selectReport.blur()
      }
    },
    // 设备树懒加载
    async loadNodedevice(node, resolve) {
      // const res = await getgenjiedianapi({ treeModel: 'MbmDeviceManageTree' })
      const res = await getgenjiedianapi({ treeModel: 'ENERGY_SET_TREE' })
      // const res = await getzijidianapi()
      const ress = await getzijidianapi(res.data.list[0])
      if (node.level === 0) {
        // 初始的级数（最顶层）
        return resolve(ress.data.list) // 最顶层数据渲染为region
      }
      if (node.level === 0) {
        const ress = await getzijidianapi(node.data)
        return resolve(ress.data.list)
      } else if (node.level === 1) {
        const ress = await getzijidianapi(node.data)
        return resolve(ress.data.list)
      } else if (node.level === 2) {
        const ress = await getzijidianapi(node.data)
        return resolve(ress.data.list)
      } else if (node.level === 3) {
        const ress = await getzijidianapi(node.data)
        return resolve(ress.data.list)
      } else if (node.level === 4) {
        const ress = await getzijidianapi(node.data)
        return resolve(ress.data.list)
      } else if (node.level === 5) {
        const ress = await getzijidianapi(node.data)
        return resolve(ress.data.list)
      }
    },
    beforeClose() {
      this.DialogVisible = false
      this.dialogForm = {}
      this.MeterPoints = { name: '', id: '' }
      this.isEdit = 'add'
    },
    // 删除
    async handleDel(item) {
      this.$confirm('此操作将永久删除该节能建议, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delPageListApi(item.id)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功',
              duration: 2000
            })
            this.getdatalist()
            this.beforeClose()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败',
              duration: 2000
            })
          }
        })
        .catch((action) => {
          if (action === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          }
        })
    }
  }
}
</script>
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
input[type='number'] {
  -moz-appearance: textfield;
}
</style>
<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-select-dropdown__item {
  padding: 0 !important;
}
.option {
  height: auto;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.bigbox {
  background-color: #f1f1f1;
  padding: 10px;
  height: 100%;
  .topbox {
    background-color: #fff;
    padding: 20px 20px 0 0px;
    box-sizing: border-box;
    height: 80px !important;
    width: 100% !important;
    // transition-property: all;
    // transition-duration: 0.2s;
    ::v-deep .el-form-item {
      width: 100%;
      margin-left: -10px;
    }
    .button {
      width: 63px;
      height: 32px;
      margin-right: 5px;
      background-color: #fff;
      border: 1px solid #ccc;
      border-radius: 3px;
    }
    .chaxun {
      background-color: #1890ff;
      color: #fff;
    }
  }
  .bodybox {
    background-color: #fff;
    margin-top: 10px;
    padding: 20px 20px 20px 20px;
    height: 90%;
    .titlebox {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      .text {
        font-weight: bold;
        font-size: 18px;
      }
      .right {
        align-items: center;
        i {
          margin-left: 10px;
        }
        .bewdao {
          width: 63px;
          height: 32px;
        }
        .daochu {
          background-color: #1890ff;
          color: #fff;
          border: none;
          border-radius: 3px;
        }
      }
    }
    .fenye {
      margin-top: 15px;
      text-align: right;
    }
  }
}
::v-deep .el-form-item__content {
  width: calc(100% - 100px);
}
::v-deep .el-col-6 {
  height: 42px;
  text-align: right;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep.el-form--inline {
  height: 100% !important;
  box-sizing: border-box;
}
::v-deep .el-input__icon {
  line-height: 20px;
}

.dropdown-list {
  padding: 0;
  margin: 0;
  text-align: center;
  width: 50px;
}
.operate {
  display: flex;
  justify-content: space-around;
}

// ::v-deep.dialog {
//   .el-dialog {
//     width: 800px;
//     // height: 550px;
//     // padding-left: 10px;
//     .el-dialog__header {
//       padding: 20px 0;
//       padding-left: 20px;
//       font-size: 14px;
//       text-align: left !important;
//       border-bottom: 1px solid #f7f7f7;
//     }
//     // .el-dialog__body {
//     //   line-height: 10px;
//     //   padding-bottom: 15px;
//     // }
//     .el-dialog__footer {
//       margin-top: 50px;
//       height: 60px;
//     }
//   }
//   //   .el-row {
//   //     height: 55px;
//   //     .el-col {
//   //       height: 55px;
//   //     }
//   //   }
// }

// ::v-deep.el-date-editor {
//   .el-range-separator {
//     line-height: 22px;
//   }
// }

// ::v-deep.dialog-content {
//   .el-form-item__error {
//     margin-top: -8px;
//   }
//   .el-form-item {
//     height: 50px;
//     .el-form-item__content {
//       width: 90%;
//       .el-select {
//         width: 100%;
//       }
//     }
//     .el-form-item__label {
//       padding: 0;
//       padding-right: 10px;
//       padding-top: 3px;
//       line-height: 32px;
//     }
//     margin-bottom: 5px;
//   }
// }
// ::v-deep.checkbox-item {
//   border-top: 1px solid #f7f7f7;
//   border-bottom: 1px solid #f7f7f7;
//   margin-top: 20px;
//   padding: 20px 0;
//   .el-form-item {
//     .el-form-item__content {
//       width: 750px;
//     }
//   }
//   .el-checkbox-group {
//     .el-checkbox {
//       margin-right: 0;
//     }
//   }
//   .yewufanwei {
//     padding-top: 10px;
//   }
// }
</style>
